import { create } from "zustand";
import { persist,devtools } from "zustand/middleware";
import { immer } from "zustand/middleware/immer";
import resetters from "@/store/resetter";
//ttypeof 获取对象类型，交于AppStoreType
type AppStoreType = typeof initstate

const initstate ={
    token:'',
    collapsed:false
}

const useAppStore = create<AppStoreType>()(
        immer(
            devtools(
                persist(
                    (set)=>{
                        //重置当前store数据
                        resetters.push(()=>set(initstate))
                        return {
                            ...initstate
                        }
                    },
                    {name:'app-store'}//持久化配置
                ),
                {name:'app-store'}//调试工具配置
            )
        )
)



export default useAppStore


//修改store数据
//token赋值
export const setToken =(token:string)=>{
    useAppStore.setState((state) =>{
        state.token = token
    })
}
//修改侧边栏状态
export const setCollapsed =(collapsed:boolean) =>{
    useAppStore.setState(state =>{
        state.collapsed = collapsed
    })
}

//------------- 选取派生数据的selector
export const selectCollapsed = (state:AppStoreType) =>state.collapsed
export const selectToken =(state:AppStoreType) =>state.token